// eslint-disable-next-line import/no-commonjs
module.exports = {
  "GET /configs/getconfig": getConfig,
  "GET /api/Default/GetCurrentTime": getCurrentTime
};

function createGuid() {
  let str = new Date().getTime().toString(16);
  do {
    str += Math.random().toString(16).replace('0.', '');
  } while (str.length < 32);
  return str.substring(0, 8) + '-' + str.substring(8, 12) + '-' + str.substring(12, 16) + '-' + str.substring(16, 20) + '-' + str.substring(20, 32);
}


function getCurrentTime(req, res) {
  try {
    res.send({ Time: new Date().getTime(), CacheId: createGuid() });
  }
  catch (ex) {
    console.log(ex)
  }
}

function getConfig(req, res) {
  try {
    const name = req.query.name;
    const pathUrl = name.replace("_", "/");

    for (var key in require.cache) delete require.cache[key];
    const pageConfig = require(`../page-configs/pages/${pathUrl}`);
    res.send(pageConfig);
  }
  catch (ex) {
    console.log(ex)
  }
}
